package com.ruoyi.project.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.domain.dto.CustomerDTO;
import com.ruoyi.project.domain.entity.Customer;
import com.ruoyi.project.mapper.CustomerMapper;
import com.ruoyi.project.service.CustomerService;
import org.springframework.stereotype.Service;

import lombok.AllArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户基本信息表(Customer)表服务实现类
 *
 * @author makejava
 * @since 2025-06-30 23:52:17
 */
@Service
@AllArgsConstructor
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    protected LambdaQueryWrapper<Customer> queryWrapper(CustomerDTO param) {
        return new LambdaQueryWrapper<Customer>()
            .like(StrUtil.isNotEmpty(param.getKeyword()), Customer::getCustomerName, param.getKeyword())
            .orderByDesc(Customer::getId);
    }

    @Override
    public Page<?> listPage(Page<Customer> page, CustomerDTO param) {
        return this.page(page, queryWrapper(param));
    }

    @Override
    public List<Customer> queryList(CustomerDTO param) {
        return this.list(queryWrapper(param));
    }

    @Override
    public CustomerDTO getDtoById(Integer id) {
        Customer entity = this.getById(id);
        CustomerDTO dto = CustomerDTO.of(entity);
        return dto;
    }

    @Override
    public List<Map<String, String>> getDict() {
        List<Customer> list = this.list();
        return new ArrayList<Map<String, String>>() {{
            for (Customer c : list) {
                add(new HashMap<String, String>() {{
                    put("name", c.getCustomerName());
                    put("code", c.getCustomerCode());
                    put("user", c.getLinkMan());
                    put("phone", c.getLinkPhone());
                    put("addr", c.getAddress());
                }});
            }
        }};
    }

    @Override
    public boolean saveOrUpdate(CustomerDTO param) {
        return param.isNew() ? this.save(param) : this.updateById(param);
    }
}
